﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtAspNet;
using MyDMS.DAL;

namespace MyDMS_ExtJS.Admin
{
    public partial class ShelfLifeEdit : System.Web.UI.Page
    {
        private int _ShelfLife_Code = 0;
        MyDMSEntity _myDmsEntity=new MyDMSEntity();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(Request.QueryString["id"]!=null)
            {
                _ShelfLife_Code = Convert.ToInt32(Request.QueryString["id"]);
            }

            if(!IsPostBack)
            {
                var sf = _myDmsEntity.ShelfLife.FirstOrDefault(s => s.ShelfLife_Code == _ShelfLife_Code);
                if (sf != null)
                {
                    tbName.Text = sf.ShelfLife_Name;
                    tbTime.Text = sf.Offset.ToString();
                }
            }
        }
        protected void btnSaveRefresh_Click(object sender, EventArgs e)
        {
            if(_ShelfLife_Code!=0)
            {
                var sf = _myDmsEntity.ShelfLife.FirstOrDefault(s => s.ShelfLife_Code == _ShelfLife_Code);
                if (sf != null)
                {
                   sf.ShelfLife_Name  = tbName.Text;
                   sf.Offset  = Convert.ToInt32(tbTime.Text.Trim());
                }
                _myDmsEntity.SaveChanges();
            }
            else
            {
                _myDmsEntity.ShelfLife.AddObject(new MyDMS.DAL.ShelfLife()
                                                     {
                                                         Offset = Convert.ToInt32(tbTime.Text.Trim()),
                                                         ShelfLife_Name = tbName.Text
                                                     });
                _myDmsEntity.SaveChanges();
            }

            // 2. 关闭本窗体，然后刷新父窗体
            PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
        }
    }
}